package com.ruoyi.business.controller;

import com.ruoyi.business.domain.Question;
import com.ruoyi.business.domain.UserExamQuestion;
import com.ruoyi.business.domain.bo.*;
import com.ruoyi.business.domain.vo.CourseHomeWorkFiVo;
import com.ruoyi.business.domain.vo.CourseHomeWorkVo;
import com.ruoyi.business.domain.vo.QuestionVo;
import com.ruoyi.business.service.IExamQuestionService;
import com.ruoyi.business.service.IHomeworkQuestionService;
import com.ruoyi.business.service.IQuestionService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.service.ISysOssService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.simpleframework.xml.Path;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 最爱喝兽奶
 * @create 2022/8/9
 **/
@RestController
@RequestMapping("/business/question")
public class QuestionController extends BaseController {

    @Autowired
    private IQuestionService questionService;

    @Autowired
    private IHomeworkQuestionService homeworkQuestionService;

    @Autowired
    private IExamQuestionService examQuestionService;

    @Autowired
    private ISysOssService ossService;


    @ApiOperation("分页查询题目信息")
    @GetMapping("/list")
    public TableDataInfo<QuestionVo> selectQuestionByPage(QuestionBo bo) {
        TableDataInfo<QuestionVo> result = questionService.selectQuestionByPage(bo);
        return result;
    }

    @ApiOperation("根据传入的作业id查询出题目信息")
    @GetMapping("/homework/{id}")
    public AjaxResult selectQuestionsByHomeworkId(@PathVariable("id") String homeworkId) {
        // 根据作业id，查询出问题id的列表
        List<String> ids = homeworkQuestionService.selectQuestionIdsByHomeworkId(homeworkId);
        List<QuestionVo> list = questionService.selectQuestionsByIds(ids);
        return AjaxResult.success(list);
    }

    @ApiOperation("根据传入的考试id，查询出考试的所有主观题")
    @GetMapping("/exam")
    public AjaxResult selectSubjectiveQuestionByExam(UserExamBo bo) {
//        List<String> questionIds = examQuestionService.selectSubjectiveQuestionsByExam(examId);
//        List<QuestionVo> list = questionService.selectQuestionsByIds(questionIds);
        List<QuestionVo> list = examQuestionService.selectSubjectiveQuestionsByExam(bo);
        return AjaxResult.success(list);
    }

    @ApiOperation("根据传入的题目id列表查询出题目信息")
    @PostMapping("/ids")
    public AjaxResult selectQuestionsByIds(@RequestBody List<String> ids) {
        List<QuestionVo> list = questionService.selectQuestionsByIds(ids);
        return AjaxResult.success(list);
    }

    @ApiOperation("按照题目的id来查询题目信息")
    @GetMapping("/{id}")
    public AjaxResult selectQuestionById(@PathVariable("id") String id) {
        QuestionVo vo = questionService.selectQuestionById(id);
        return AjaxResult.success(vo);
    }

    @ApiOperation("导入题目")
    @PostMapping("/batch")
    public AjaxResult importQuestionByCourseId(@RequestParam("file")MultipartFile file,
                                               @RequestParam("courseId") String courseId) throws Exception {
        ExcelUtil<QuestionBo> util = new ExcelUtil<QuestionBo>(QuestionBo.class);
        List<QuestionBo> boList = util.importExcel(file.getInputStream());
        questionService.insertQuestionByCourseId(boList, courseId);
        return AjaxResult.success();
    }

    @ApiOperation("导入题目信息")
    @PostMapping("/import")
    public AjaxResult importQuestionByCourseId(@RequestBody Map<String, Object> map) throws Exception {
        if (null != map.get("file")) {
            ExcelUtil<QuestionBo> util = new ExcelUtil<QuestionBo>(QuestionBo.class);
            List<QuestionBo> boList = util.importExcel(ossService.getInputStreamByOssId((Serializable) map.get("file")));
            Object courseId = map.get("courseId");
            // 设置题型
            boList = boList.stream().map(item -> {
                Object type = map.get("questionType");
                if (type instanceof String) {
                    item.setType((String) type);
                } else if (type instanceof Long) {
                    item.setType(String.valueOf(type));
                }
                return item;
            }).collect(Collectors.toList());
            if (courseId instanceof Long) {
                questionService.insertQuestionByCourseId(boList, String.valueOf(courseId));
            }else if (courseId instanceof String) {
                questionService.insertQuestionByCourseId(boList, (String) courseId);
            }
            return AjaxResult.success();
        } else {
            return AjaxResult.error("请传入文件信息");
        }
    }

    @ApiOperation("拿到课程作业题")
    @GetMapping("/listNoAnswer/{id}")
    public AjaxResult getHomeWorkByCourseId(@PathVariable("id") String id) throws Exception {
        List<CourseHomeWorkVo> list = questionService.getHomeWorkByCourseIdNoAnswer(id);
        return AjaxResult.success(list);
    }


    @ApiOperation("提交课程作业题")
    @PostMapping("/submitCourseAnswers/{id}")
    public AjaxResult submitCourseAnswers(@PathVariable("id") String id,
                                          @RequestBody List<CourseHomeWorkVo> list) throws Exception {
        Boolean   flag = questionService.submitCourseAnswers(list,id);
        if (flag) {
            return AjaxResult.success("提交成功");
        } else {
            return AjaxResult.error("提交失败");

        }
    }

    @ApiOperation("查看已完成课程作业详情")
    @GetMapping("/queryFinishedHomeWork")
    public AjaxResult queryFinishedHomeWork(@Param("courseId") String courseId,
                                            @Param("writerId") String writerId) throws Exception {
        FinishedHomeWorkBo bo = new FinishedHomeWorkBo();
        bo.setCourseId(courseId);
        bo.setWriterId(writerId);
        List<CourseHomeWorkVo> list = questionService.getHomeWorkByCourseIdWithAnswer(bo);
//        List<CourseHomeWorkFiVo> list2 = new ArrayList<>();
//        list.forEach(e -> {
//            CourseHomeWorkFiVo fi = new CourseHomeWorkFiVo();
//            BeanUtils.copyProperties(e,fi);
//            if (StringUtils.isNotNull(e.getStuAnswer()) && "0".equals(e.getType())) {
//                fi.setStuAnswer(Integer.valueOf(e.getStuAnswer()));
//            }
//            if (StringUtils.isNotNull(e.get) && "0".equals(e.getType())) {
//                fi.setStuAnswer(Integer.valueOf(e.getStuAnswer()));
//            }
//
//            list2.add(fi);
//
//
//        });
        return AjaxResult.success(list);


    }




    @ApiOperation("插入题目")
    @PostMapping
    public AjaxResult addQuestion(@RequestBody QuestionBo bo) {
        questionService.insertQuestionByCourseId(bo);
        return AjaxResult.success();
    }

    @ApiOperation("修改插入的题目")
    @PutMapping
    public AjaxResult updateQuestion(@RequestBody QuestionBo bo) {
        questionService.updateQuestionByCourseId(bo);
        return AjaxResult.success();
    }

    @ApiOperation("删除题目")
    @DeleteMapping("/{id}")
    public AjaxResult deleteQuestion(@PathVariable("id") String id) {
        questionService.deleteQuestionById(id);
        return AjaxResult.success();
    }
}
